const request = require('./request')
const MS = require("./ms.js")
const Error = require('./error')

const pay = class pay {

    constructor() {
    }

    weApp (orderId, price, productName, body) {
        var user = MS.User.current();
        if(!user) {
            throw new Error(416, "当前用户未登录")
        }
        if (!price || !productName || !body || !orderId) {
            throw new Error(416)
        }
        var data = { 'userId': user.id, 'orderId': orderId, 'totalFee': price, 'detail': productName, 'body': body }
        console.info(data)
        return new Promise((resolve, reject) => {
            request(MS._config.parameters.APP.WX_CREATE_UNIFIED_ORDER.url,
                MS._config.parameters.APP.WX_CREATE_UNIFIED_ORDER.method, data).then(res => {
                resolve(res.data)
            }).catch(err => {
                reject(err);
            })
        })
    }
}

module.exports = pay